package com.heima.kafka.boot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author DiXiaoNie
 * @since 2022/12/28 16:17
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/send")
    public String sendMessage() {
        //3.发送消息
        /**
         * ProducerRecord(String topic, K key, V value)
         * 参数1表示发送消息的主题，也叫队列名称
         * 参数2表示发送消息的key
         参数3表示发送的消息内容
         */
        for (int i = 0; i < 5; i++) {
            if (i % 2 == 0) {
                kafkaTemplate.send("kafka-stream-producer", "hello boot kafka");
            } else {
                kafkaTemplate.send("kafka-stream-producer", "hello itcast");
            }
        }

        return "发送成功";
    }
}
